<?php
/**
 * Created by PhpStorm.
 * User: wangjie
 * Date: 2018/11/18
 * Time: 11:51
 */
namespace api\controllers;

use Yii;
use common\services\JuHeDataService;


class TotalDataController extends BaseController
{
    public function actionInlet()
    {
        $auto       = new JuHeDataService();
        /***** 新闻 *****/
        $newsData = $auto->findNewsHotspot();
        // 判断当前时间段是否存在新闻
        if (empty($newsData)) {
            $newsResult = $auto->getNewsHotspot();
            $newsData = $auto->saveNewsHotspot($newsResult);
        }

        /***** 汇率 *****/
        $exchangeRateData = $auto->findExchangeRate();
        // 判断当前时间段汇率是否更新
        if (empty($exchangeRateData)) {
            $exchangeRateResult = $auto->getExchangeRate();
            $exchangeRateData = $auto->saveExchangeRate($exchangeRateResult);
        }

        /***** 历史上的今天 *****/
        $historyData = $auto->findHistory();
        // 判断当前时间段汇率是否更新
        if (empty($historyData)) {
            $historyResult = $auto->getHistory();
            $historyData = $auto->saveHistory($historyResult);
        }

        $result = [
            'news_list'             =>  $newsData,
            'exchange_rate_list'    =>  $exchangeRateData,
            'history_list'          =>  $historyData,
        ];
        return $this->endReponse('0',$result);
    }
}
